<cfsetting enablecfoutputonly="true">

<cfset Root="d:/inetpub/wwwroot/nowrists">
<cfset Args=ListToArray(CGI.PATH_INFO,"/")>
<cfset UID=0>
<cfset MapTitle="">
<cfif (ArrayLen(Args) GT 0) AND IsNumeric(Args[1])>
	<cfset UID=Abs(Int(Args[1]))>
	<cfset ArrayDeleteAt(Args,1)>
	<cfquery datasource="nowrists" name="User">
	SELECT username
	FROM nws_nano_userinfo
	WHERE (uid = <cfqueryparam cfsqltype="cf_sql_integer" value="#UID#">)
	</cfquery>
	<cfif User.RecordCount EQ 1>
		<cfset MapTitle=User.Username & "'">
		<cfif Right(User.Username,1) NEQ "s"><cfset MapTitle=MapTitle & "s"></cfif>
		<cfset MapTitle=MapTitle & " global visibility">
	<cfelse>
		<cfset UID=0>
	</cfif>
</cfif>
<cfset MapImage=Root & "/dyn/map/wm#UID#.jpg">
<cflock name="NaNoWorldMap#UID#" timeout="30">
	<cfset NeedBuild=True>
	<cfif FileExists(MapImage)>
		<cfdirectory action="list" directory="#Root#/dyn/map" filter="wm#UID#.jpg" name="FindMap">
		<cfif (FindMap.RecordCount EQ 1) AND IsDate(FindMap.DateLastModified) AND (DateAdd("n",30,FindMap.DateLastModified) GTE Now())>
			<cfset NeedBuild=False>
		</cfif>
	</cfif>
	<cfif NeedBuild>
		<cfquery datasource="nowrists" name="Countries">
		SELECT cc, COUNT(DISTINCT ip_int) AS HowMany
		FROM nws_graph_refs
		<cfif UID GT 0>
		WHERE (uid = <cfqueryparam cfsqltype="cf_sql_integer" value="#UID#">)
		</cfif>
		GROUP BY cc
		</cfquery>
		<cfset CountryFill=StructNew()>
		<cfset MaxCount=ArrayMax(ListToArray(ValueList(Countries.HowMany)))>
		<cfset FillColors=ListToArray("d5e5ff,aaccff,80b3ff,5599ff,2a7ff,0066ff,0055d4,0044aa,003380,002255,00112b")>
		<cfloop query="Countries">
			<cfset CountryFill[cc]=FillColors[IncrementValue(Round(10*HowMany/MaxCount))]>
		</cfloop>
		<cfset munger=CreateObject("component","com.nowrists.SVGMunger").init("#Root#/dyn/svg/WorldMapIDs2.svg")>
		<cfif MapTitle NEQ "">
			<cfset munger.ReplaceIdContent("title",MapTitle)>
		</cfif>
		<cfset ids=XMLSearch(munger.XML,"/descendant::*[attribute::id]")>
		<cfloop from="1" to="#ArrayLen(ids)#" index="i">
			<cfset idcc=ids[i].XmlAttributes.id>
			<cfif StructKeyExists(CountryFill,idcc)>
				<cfset StructInsert(ids[i].XmlAttributes,"fill","###CountryFill[idcc]#",true)>
			</cfif>
		</cfloop>
		<cfset SVGFile=GetTempFile(GetTempDirectory(),"swm")>
		<cfset munger.SaveAs(SVGFile)>
		<cfset Rend=CreateObject("component","com.nowrists.SVGRenderer")>
		<cfset Rend.RenderFile(SVGFile,MapImage,"","jpeg90")>
		<cftry>
			<cffile action="delete" file="#SVGFile#">
			<cfcatch></cfcatch>
		</cftry>
	</cfif>
</cflock>
<cfcontent file="#MapImage#" type="image/jpeg">